<div class="eui-layout" data-options="fit:true">
	<div data-options="region:'west',split:true" style="width:255px;">
		<ul class="eui-tree" id="tree"
			data-options="url:'${path}/eframe/framework/application/app!getTreedata.action',
			method:'post',
			animate:false,
			onBeforeLoad:addParam,
			onContextMenu:addMenu,
			onClick:requestTab,
			onLoadSuccess:expandRoot"></ul>
		<div id="rootMenu${m}" class="eui-menu" style="width:120px;">			<div data-options="iconCls:'icon-add'" id="appaddbtn${m}">添加应用</div>
			<div class="menu-sep"></div>
			<div data-options="iconCls:'icon-reload'" id="rootreloadbtn${m}">刷新</div>
		</div>
		<div id="appMenu${m}" class="eui-menu" style="width:120px;">
			<div data-options="iconCls:'icon-add'" id="groupaddbtn${m}">添加功能组</div>
			<div class="menu-sep"></div>
			<div data-options="iconCls:'icon-edit'" id="appupdatebtn${m}">修改应用</div>
			<div data-options="iconCls:'icon-remove'" id="appremovebtn${m}">删除应用</div>
			<div class="menu-sep"></div>
			<div data-options="iconCls:'icon-reload'" id="appreloadbtn${m}">刷新</div>
		</div>
		<div id="funcgroupMenu${m}" class="eui-menu" style="width:120px;">
			<div data-options="iconCls:'icon-add'" id="subgroupaddbtn${m}">添加子功能组</div>
			<div class="menu-sep"></div>
			<div data-options="iconCls:'icon-add'" id="funcaddbtn${m}">添加功能</div>
			<div class="menu-sep"></div>
			<div data-options="iconCls:'icon-edit'" id="groupupdatebtn${m}">修改功能组</div>
			<div data-options="iconCls:'icon-remove'" id="groupremovebtn${m}">删除功能组</div>
			<div class="menu-sep"></div>
			<div data-options="iconCls:'icon-reload'" id="groupreloadbtn${m}">刷新</div>
		</div>
	</div>
	<div data-options="region:'center',href:'${path}/eframe/framework/application/root-tabs.ftl',border:false" id="workspace${m}"></div>
</div>
<script>
	//添加需要传递的参数
	function addParam(node, param) {
		if(node && node.attributes && node.attributes.nodeType) {
			param.nodeType = node.attributes.nodeType;
		}
	}
	
	function expandRoot(node, data) {
		if(!node) {
			$("#tree").tree('expandAll');
		}
	}
	
	//显示树菜单
	function addMenu(e,node) {
		e.preventDefault();
		$("#tree").tree('select',node.target);
		$('#'+node.attributes.nodeType+'Menu${m}').menu('show',{
			left: e.pageX,
			top: e.pageY
		});
	}
	
	function reloadSelected() {
		var node = $("#tree").tree('getSelected');
		$("#tree").tree('reload',node.target);
		requestTab(node);
	}
	function reloadSelectedParent() {
		var node = $("#tree").tree('getSelected');
		node = $("#tree").tree('getParent',node.target);
		$("#tree").tree('select',node.target)
		$("#tree").tree('reload',node.target);
		requestTab(node);
	}
	
	function requestTab(node) {
		var url;
		if(node.attributes.nodeType == "root") {
			url = "${path}/eframe/framework/application/root-tabs.ftl";
		} else if(node.attributes.nodeType == "app") {
			url = "${path}/eframe/framework/application/tabs.ftl?appid="+node.id;
		} else if(node.attributes.nodeType == "funcgroup") {
			var parent = $("#tree").tree('getParent',node.target);
			while(parent.attributes.nodeType != 'app') {
				parent = $("#tree").tree('getParent',parent.target);
			}
			var newGroupLevel = parseInt(node.attributes.groupLevel) + 1;
			url = "${path}/eframe/framework/funcgroup/tabs.ftl?appid="+parent.id+"&groupid="+node.id+"&grouplevel="+newGroupLevel;
		} else if(node.attributes.nodeType == "func") {
			url = "${path}/eframe/framework/function/tabs.ftl?funcid="+node.id;
		}
		$('#workspace${m}').panel('open').panel('refresh', url);
	}
	
	
	
	
	//菜单点击事件
	$("#appaddbtn${m}").click(function(){
		dialog.open({
			width:600,
			height:300,
			url:'${path}/eframe/framework/application/add.ftl',
			success:reloadSelected
		});
	});
	$("#groupaddbtn${m}").click(function(){
		var node = $("#tree").tree('getSelected');
		dialog.open({
			width:600,
			height:300,
			url:'${path}/eframe/framework/funcgroup/add.ftl?appid='+node.id+'&grouplevel=1',
			success:reloadSelected
		});
	});
	$("#appupdatebtn${m}").click(function(){
		var node = $("#tree").tree('getSelected');
		dialog.open({
			width:600,
			height:300,
			url:'${path}/eframe/framework/application/app!updateView.action?id='+node.id,
			success:reloadSelectedParent
		});
	});
	$("#appremovebtn${m}").click(function(){
		$("#tree").tree('deleteSelected',{
			url:'${path}/eframe/framework/application/app!delete.action',
			success:function(node) {
				requestTab(node);
			}
		});
	});
	$("#subgroupaddbtn${m}").click(function(){
		var node = $("#tree").tree('getSelected');
		var parent = $("#tree").tree('getParent',node.target);
		while(parent.attributes.nodeType != 'app') {
			parent = $("#tree").tree('getParent',parent.target);
		}
		var newGroupLevel = parseInt(node.attributes.groupLevel) + 1;
		dialog.open({
			width:600,
			height:300,
			url:'${path}/eframe/framework/funcgroup/add.ftl?appid='+parent.id+'&groupid='+node.id+'&grouplevel='+newGroupLevel,
			success:reloadSelected
		});
	});
	$("#funcaddbtn${m}").click(function(){
		var node = $("#tree").tree('getSelected');
		var parent = $("#tree").tree('getParent',node.target);
		while(parent.attributes.nodeType != 'app') {
			parent = $("#tree").tree('getParent',parent.target);
		}
		dialog.open({
			width:600,
			height:300,
			url:'${path}/eframe/framework/function/add.ftl?appid='+parent.id+'&groupid='+node.id,
			success:reloadSelected
		});
	});
	
	$("#groupupdatebtn${m}").click(function(){
		var node = $("#tree").tree('getSelected');
		dialog.open({
			width:600,
			height:300,
			url:'${path}/eframe/framework/funcgroup/group!updateView.action?id='+node.id,
			success:reloadSelectedParent
		});
	});
	$("#groupremovebtn${m}").click(function(){
		$("#tree").tree('deleteSelected',{
			url:'${path}/eframe/framework/funcgroup/group!delete.action',
			success:function(node) {
				requestTab(node);
			}
		});
	});
	
	$("#rootreloadbtn${m}").click(function(){
		reloadSelected();
	});
	$("#appreloadbtn${m}").click(function(){
		reloadSelected();
	});
	$("#groupreloadbtn${m}").click(function(){
		reloadSelected();
	});
	
</script>